<script>
import {createPicking} from "@/api/order";

export default {
  name: "PacketGeneratePickingBtn1",
  props: {
    selectOrderIds:{
      type: Array,
      required: true,
    },
    shippingType: {
      type: Number,
      default: 0, // 0:无面单 1:平台面单
    }
  },
  computed: {
    disable(){
      return this.selectOrderIds.filter(item => item.shippingType === this.shippingType).length === 0;
    },
    orderIds(){
      return this.selectOrderIds.filter(item => item.shippingType === this.shippingType).map(item => item.orderId)
    },
    btnText(){
      return this.shippingType === 1 ? '生成拣货单(平台面单)' : '生成拣货单(物流面单)';
    }
  },
  data(){
    return {
      loading: false,
    }
  },
  methods: {
    createPicking(){
      const postData = {
        orderIds: this.orderIds,
        accountTypeNum: 7,
        shippingType: this.shippingType,
      }
      this.printLoading = true
      createPicking(postData)
        .then(res=>{
          this.printLoading = false
          if(res.error !== 0){
            this.$errorMessage(res.message)
          }else{
            const pickingNo = res.data.pickingNo
            let routeUrl = this.$router.resolve({
              name: 'packInfo',
              query: {
                'pickingNo' : pickingNo,
              }
            })
            // 新标签打开路由
            window.open(routeUrl.href, '_blank');
            // 刷新当前页面
            this.$emit('createPicking')
          }
        })
    }
  }
}
</script>

<template>
  <el-button type="primary" @click="createPicking" :loading="loading" :disabled="disable">{{ btnText }}</el-button>
</template>

<style scoped lang="scss">

</style>
